package com.serviceprovider.business.module.admin.user.controller;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.module.admin.role.model.Role;
import com.serviceprovider.business.module.admin.role.services.dao.interfaces.IRoleService;
import com.serviceprovider.business.module.admin.user.validator.RoleValidator;
import com.serviceprovider.business.utility.AppUtil;
import com.serviceprovider.business.utility.ApplicationConfigProperties;
import com.serviceprovider.business.utility.RequestConstants;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

//~--- JDK imports ------------------------------------------------------------

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
public class RoleController {
    @Autowired
    ApplicationConfigProperties applicationConfigProperties;
    @Autowired
    IRoleService                roleService;
    @Autowired
    RoleValidator               validator;

    @RequestMapping(value = RequestConstants.UserRequests.ROLE)
    public ModelAndView enterRole() {
        ModelAndView mav = new ModelAndView(RequestConstants.UserRequests.VIEW_ROLE);

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.UserRequests.CREATE_ROLE,
        method = RequestMethod.GET
    )
    public ModelAndView enterCreateRole(HttpSession sess) {
        ModelAndView mav = new ModelAndView(RequestConstants.UserRequests.VIEW_CREATE_ROLE);

        mav.addObject("role", new Role());

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.UserRequests.CREATE_ROLE,
        method = RequestMethod.POST
    )
    public ModelAndView createRole(@ModelAttribute("role") Role form, BindingResult result) {
        validator.validate(form, result);

        if (result.hasErrors()) {
            ModelAndView mav = new ModelAndView(RequestConstants.UserRequests.CREATE_ROLE_VIEW);

            mav.addObject("role", form);

            return mav;
        }

        ModelAndView mav = new ModelAndView("redirect:" + RequestConstants.UserRequests.VIEW_ROLES);

        roleService.insertRole(form);

        return mav;
    }

    @RequestMapping(value = RequestConstants.UserRequests.VIEW_ROLES)
    public ModelAndView viewRoles(HttpServletRequest request, HttpSession sess) {
        ModelAndView mav        = new ModelAndView(RequestConstants.UserRequests.VIEW_ROLES_VIEW);
        int          recordSize = applicationConfigProperties.getRecordSize("view_roles.records.size");
        int          pageNo     = 1;

        try {
            pageNo = Integer.parseInt(request.getParameter("pageNo"));
        } catch (NumberFormatException ex) {}

        String sortOrder = ((request.getParameter("sortOrder") == null)
                            || (request.getParameter("sortOrder").length() == 0))
                           ? "asc"
                           : request.getParameter("sortOrder");
        String sortColumn = ((request.getParameter("sortColumn") == null)
                             || (request.getParameter("sortColumn").length() == 0))
                            ? "id"
                            : request.getParameter("sortColumn");

        mav.addObject("roleList", roleService.findAll(pageNo, recordSize, sortColumn, sortOrder));

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.UserRequests.EDIT_ROLE,
        method = RequestMethod.GET
    )
    public ModelAndView enterEditRole(HttpServletRequest req, HttpSession sess) {
        ModelAndView mav = new ModelAndView(RequestConstants.UserRequests.EDIT_ROLE_VIEW);

        mav.addObject("role", roleService.getRoleById(Integer.parseInt(req.getParameter("id"))));

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.UserRequests.EDIT_ROLE,
        method = RequestMethod.POST
    )
    public ModelAndView editRole(@ModelAttribute("role") Role form) {
        ModelAndView mav = new ModelAndView("redirect:" + RequestConstants.UserRequests.VIEW_ROLES);

        roleService.updateRole(form);

        return mav;
    }

    @RequestMapping(
        value  = RequestConstants.UserRequests.DELETE_ROLES,
        method = RequestMethod.POST
    )
    public ModelAndView deleteRoles(HttpServletRequest req) {
        ModelAndView mav = new ModelAndView("redirect:" + RequestConstants.UserRequests.VIEW_ROLES);

        roleService.deleteRolesById(AppUtil.toInt(req.getParameterValues("ids")));

        return mav;
    }

    @RequestMapping(value = RequestConstants.UserRequests.UPDATE_ROLES)
    public ModelAndView updateStatus(HttpServletRequest req) {
        ModelAndView mav = new ModelAndView("redirect:" + RequestConstants.UserRequests.VIEW_ROLES);

        roleService.updateStatus(Integer.parseInt(req.getParameter("id")), "true".equals(req.getParameter("active")));

        return mav;
    }
}



